<template>
  <div>
    <div class="top-panel__box">
      <el-form :model="dataForm" ref="dataForm" inline size="small" onsubmit="return false">
        <el-form-item>
          <el-input placeholder="姓名/手机/工号" v-model="dataForm.freeCondition" maxlength="30" clearable />
        </el-form-item>
        <el-form-item label="所属机构" label-width="80px">
            <organization-cascader :flag="'id'" v-model="dataForm.organizationId"></organization-cascader>
        </el-form-item>
        <el-form-item label="选择职务" label-width="80px">
            <el-select placeholder="选择职务" v-model="dataForm.roleIdList" multiple clearable collapse-tags>
              <el-option v-for="(item) in roleList" :key="item.roleId" :value="item.roleId" :label="item.roleName" />
            </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList(1)">
            <fa-icon class="fa-search" />查询</el-button>
          <el-button plain type="" @click="onReset">
            <fa-icon class="fa-refresh" />重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="top-panel__box">
        <el-form onsubmit="return false" size="small">
          <el-form-item>
            <el-button type="primary" @click="editUser = true" v-permission="'BEFORE_USER_ADD'">
                <fa-icon class="el-icon-plus" />新增调查专员
            </el-button>
          </el-form-item>
        </el-form>
    </div>
    <el-row class="table-top">
      <el-table :border="true" :data="dataList" v-loading="tableLoading" tooltip-effect="light" class="tc-table" size="small">
        <el-table-column type="index" width="55" fixed="left" label="序号"></el-table-column>
        <template v-for="(column, index) in FOLLOW_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
        <el-table-column fixed="right" class-name="oper-col" width="250" label="操作">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" plain @click="deletPersonInfo(scope.row)" v-permission="'BEFORE_USER_DELETE'">
                移除
              </el-button>
            </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination-top">
        <pagination :pageNum.sync="dataForm.pageNum" :curSizes.sync="dataForm.pageSize" :total="total" @getList="getList" />
      </el-row>
    </el-row>
    <el-dialog title="编辑成员" :visible.sync="editUser" width="60%" :close-on-click-modal="false">
      <choose-user v-if="editUser" :valueList="userValueList" @updataSuccess="getUserData" @close="closeUserChoose" />
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import api from '../api'
import { CONSTANT_OBJ } from '@/utils/constant'
import { FOLLOW_HEAD_LIST } from '../js/contant.js'
import OrganizationCascader from '@/components/organizationCascader'
import { common } from '@/utils/common'
import ChooseUser from './ChooseUser'
export default {
  components: {OrganizationCascader, ChooseUser},
  data () {
    return {
      FOLLOW_HEAD_LIST,
      tableLoading: false,
      editUser: false,
      dataForm: {
        freeCondition: '',
        organizationId: '',
        organizationName: '',
        roleIdList: [],
        roleName: '',
        pageNum: 0,
        pageSize: 10
      },
      userValueList: [],
      dataList: [],
      editorDialog: false,
      total: 0,
      products: [
        { productId: '1', productName: '即享金', productNo: 101 },
        { productId: '2', productName: '即享贷', productNo: 102 }
      ],
      editData: {},
      uploadDialog: false,
      roleList: []
    }
  },
  mounted () {
    this.getList()
    this.getRoleList()
  },
  methods: {
    // 查询列表
    getList (val) {
      this.dataForm.pageNum = val || this.dataForm.pageNum
      this.tableLoading = true
      api.queryAcceptorList(this.dataForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataList = data.list || []
          this.dataList.forEach(v => {
            v.newGender = this.dicts['gender'].childMap[v.gender]
          })
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 查询职务
    getRoleList () {
      api.postQueryRoleData({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.roleList = data.list || []
        }
      })
    },
    showEditor (row) {
      this.editorDialog = true
      this.editData = {...row}
    },
    close (data) {
      console.log(data)
      this.editorDialog = data
    },
    deletPersonInfo (row) {
      common.confirmFn(`确认移除调查专员 ${row.realName}(${row.userName})`, '移除调查专员确认').then(res => {
        if (res) {
          api.deleteAcceptor(row.userId).then(res => {
            const { responseCode } = res
            if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
              this.getList(0)
              console.log('删除成功')
            }
          })
        }
      })
    },
    showUpload () {
      this.uploadDialog = true
    },
    closeUpload (res) {
      this.uploadDialog = res
    },
    getUserData (data) {
      const userIds = data.map(ele => ele.userId)
      console.log(userIds)
      api.addAcceptor(userIds).then(res => {
        const { responseCode } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.editUser = false
          this.getList(1)
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    closeUserChoose (val) {
      this.editUser = val
    },
    onReset () {
      this.dataForm = {
        freeCondition: '',
        organizationId: '',
        organizationName: '',
        roleIdList: [],
        roleName: '',
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  computed: {
    ...mapState({
      dicts: state => state.common.dicts
    })
  }
}
</script>
